<template>
<div>
  <br>
  <el-form :inline="true">
          <el-form-item label="createDate">
<el-input v-model="searchMap.createDate" placeholder="createDate"></el-input></el-form-item>
          <el-form-item label="modifyDate">
<el-input v-model="searchMap.modifyDate" placeholder="modifyDate"></el-input></el-form-item>
          <el-form-item label="createUser">
<el-input v-model="searchMap.createUser" placeholder="createUser"></el-input></el-form-item>
          <el-form-item label="modifyUser">
<el-input v-model="searchMap.modifyUser" placeholder="modifyUser"></el-input></el-form-item>
          <el-form-item label="name">
<el-input v-model="searchMap.name" placeholder="name"></el-input></el-form-item>
          <el-form-item label="优惠金额">
<el-input v-model="searchMap.discountPrice" placeholder="优惠金额"></el-input></el-form-item>
          <el-form-item label="使用满足条件">
<el-input v-model="searchMap.usePrice" placeholder="使用满足条件"></el-input></el-form-item>
          <el-form-item label="有效期开始时间">
<el-input v-model="searchMap.startDate" placeholder="有效期开始时间"></el-input></el-form-item>
          <el-form-item label="有效期结束时间">
<el-input v-model="searchMap.endDate" placeholder="有效期结束时间"></el-input></el-form-item>
          <el-form-item label="总投放数量  -1代表不限制">
<el-input v-model="searchMap.putTotalNum" placeholder="总投放数量  -1代表不限制"></el-input></el-form-item>
          <el-form-item label="没人限制领取数量  -1 不限制">
<el-input v-model="searchMap.peopleLimit" placeholder="没人限制领取数量  -1 不限制"></el-input></el-form-item>
          <el-form-item label="已领取数量">
<el-input v-model="searchMap.receiveNum" placeholder="已领取数量"></el-input></el-form-item>
          <el-form-item label="0线上  1线下  ">
<el-input v-model="searchMap.type" placeholder="0线上  1线下  "></el-input></el-form-item>
          <el-form-item label="sort">
<el-input v-model="searchMap.sort" placeholder="sort"></el-input></el-form-item>
          <el-form-item label="0  不启用   1启用">
<el-input v-model="searchMap.status" placeholder="0  不启用   1启用"></el-input></el-form-item>
          <el-form-item label="0没有设置  1限量 2限领">
<el-input v-model="searchMap.property" placeholder="0没有设置  1限量 2限领"></el-input></el-form-item>

    <el-button type="primary" @click="fetchData()">查询</el-button>
    <el-button type="primary" @click="handleEdit('')">新增</el-button>
  </el-form>
  <el-table
    :data="list"
    border
    style="width: 100%">
          <el-table-column prop="id" label="id" width="80"></el-table-column>
          <el-table-column prop="createDate" label="createDate" width="80"></el-table-column>
          <el-table-column prop="modifyDate" label="modifyDate" width="80"></el-table-column>
          <el-table-column prop="createUser" label="createUser" width="80"></el-table-column>
          <el-table-column prop="modifyUser" label="modifyUser" width="80"></el-table-column>
          <el-table-column prop="name" label="name" width="80"></el-table-column>
          <el-table-column prop="discountPrice" label="优惠金额" width="80"></el-table-column>
          <el-table-column prop="usePrice" label="使用满足条件" width="80"></el-table-column>
          <el-table-column prop="startDate" label="有效期开始时间" width="80"></el-table-column>
          <el-table-column prop="endDate" label="有效期结束时间" width="80"></el-table-column>
          <el-table-column prop="putTotalNum" label="总投放数量  -1代表不限制" width="80"></el-table-column>
          <el-table-column prop="peopleLimit" label="没人限制领取数量  -1 不限制" width="80"></el-table-column>
          <el-table-column prop="receiveNum" label="已领取数量" width="80"></el-table-column>
          <el-table-column prop="type" label="0线上  1线下  " width="80"></el-table-column>
          <el-table-column prop="sort" label="sort" width="80"></el-table-column>
          <el-table-column prop="status" label="0  不启用   1启用" width="80"></el-table-column>
          <el-table-column prop="property" label="0没有设置  1限量 2限领" width="80"></el-table-column>

    <el-table-column
      fixed="right"
      label="操作"
      width="100">
      <template slot-scope="scope">
        <el-button @click="handleEdit(scope.row.id)" type="text" size="small">修改</el-button>
        <el-button @click="handleDelete(scope.row.id)" type="text" size="small">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
      <el-pagination
      @size-change="fetchData"
      @current-change="fetchData"
      :current-page="currentPage"
      :page-sizes="[5,10,20]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>  
  <el-dialog title="编辑" :visible.sync="dialogFormVisible">
    <el-form label-width="80px">
        <el-form-item label="createDate"><el-input v-model="pojo.createDate"></el-input></el-form-item>
        <el-form-item label="modifyDate"><el-input v-model="pojo.modifyDate"></el-input></el-form-item>
        <el-form-item label="createUser"><el-input v-model="pojo.createUser"></el-input></el-form-item>
        <el-form-item label="modifyUser"><el-input v-model="pojo.modifyUser"></el-input></el-form-item>
        <el-form-item label="name"><el-input v-model="pojo.name"></el-input></el-form-item>
        <el-form-item label="优惠金额"><el-input v-model="pojo.discountPrice"></el-input></el-form-item>
        <el-form-item label="使用满足条件"><el-input v-model="pojo.usePrice"></el-input></el-form-item>
        <el-form-item label="有效期开始时间"><el-input v-model="pojo.startDate"></el-input></el-form-item>
        <el-form-item label="有效期结束时间"><el-input v-model="pojo.endDate"></el-input></el-form-item>
        <el-form-item label="总投放数量  -1代表不限制"><el-input v-model="pojo.putTotalNum"></el-input></el-form-item>
        <el-form-item label="没人限制领取数量  -1 不限制"><el-input v-model="pojo.peopleLimit"></el-input></el-form-item>
        <el-form-item label="已领取数量"><el-input v-model="pojo.receiveNum"></el-input></el-form-item>
        <el-form-item label="0线上  1线下  "><el-input v-model="pojo.type"></el-input></el-form-item>
        <el-form-item label="sort"><el-input v-model="pojo.sort"></el-input></el-form-item>
        <el-form-item label="0  不启用   1启用"><el-input v-model="pojo.status"></el-input></el-form-item>
        <el-form-item label="0没有设置  1限量 2限领"><el-input v-model="pojo.property"></el-input></el-form-item>

        <el-button type="primary" @click="handleSave()">保存</el-button>
        <el-button @click="dialogFormVisible = false" >关闭</el-button>
    </el-form>
  </el-dialog>
</div>
</template>
<script>
import couponApi from '@/api/coupon'
export default {
  data() {
    return {
      list: [],
      total: 0, // 总记录数
      currentPage: 1, // 当前页
      pageSize: 10, // 每页大小
      searchMap: {}, // 查询条件
      dialogFormVisible: false, // 编辑窗口是否可见
      pojo: {}, // 编辑表单绑定的实体对象
      cityList: [], // 城市列表
      id: '' // 当前用户修改的ID
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      couponApi.search(this.currentPage, this.pageSize, this.searchMap).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
      })
    },
    handleSave() {
      couponApi.update(this.id, this.pojo).then(response => {
        this.$message({
          message: response.message,
          type: (response.flag ? 'success' : 'error')
        })
        if (response.flag) { // 如果成功
          this.fetchData() // 刷新列表
        }
      })
      this.dialogFormVisible = false // 关闭窗口
    },
    handleEdit(id) {
      this.id = id
      this.dialogFormVisible = true // 打开窗口
      if (id !== '') { // 修改
        couponApi.findById(id).then(response => {
          if (response.flag) {
            this.pojo = response.data
          }
        })
      } else {
        this.pojo = {} // 清空数据
      }
    },
    handleDelete(id) {
      this.$confirm('确定要删除此纪录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        couponApi.deleteById(id).then(response => {
          this.$message({ message: response.message, type: (response.flag ? 'success' : 'error') })
          if (response.flag) {
            this.fetchData() // 刷新数据
          }
        })
      })
    }
  }
}
</script>
